//
//  HEConsulItemViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/23.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEConsulItemViewController.h"
#import "HEConsulItemCell.h"

#import "HEAIDiesaeMsgModel.h"
#import "HEAIConsultModel.h"

@interface HEConsulItemViewController ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, assign) BOOL hasLoad;
 
@property (nonatomic, strong) NSMutableArray <HEAIConsultModel *> *personConsArr; // 疾病自诊

@end

@implementation HEConsulItemViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"免费咨询";
    
    _personConsArr = @[].mutableCopy;
    
    [self initUI];
    
    [self loadData];
}

- (void)initUI {
    
  
    // 1.创建流水布局
    _layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.scrollDirection = UICollectionViewScrollDirectionVertical;
//    _layout.estimatedItemSize = CGSizeMake(120, 100);

    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
    _collectionView.backgroundColor = KWhiteColor;
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    [self.view addSubview:_collectionView];
    
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).mas_offset(ZXNavBarHeight);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).mas_offset(-7);
    }];
    
    /// 设置此属性为yes 不满一屏幕 也能滚动
    self.collectionView.alwaysBounceVertical = YES;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    [_collectionView registerClass:HEConsulItemCell.class forCellWithReuseIdentifier: KHEConsulItemCell];
 
    
}
 
- (void)loadData {
//    Kservice_all_ask
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:Kservice_all_ask parameters: @{} success:^(id  _Nonnull responseObject) {

        HEArrayResponse *response = [HEArrayResponse mj_objectWithKeyValues:responseObject];
        [self.personConsArr addObjectsFromArray:[HEAIConsultModel mj_objectArrayWithKeyValuesArray: response.data]];
       
        [self.collectionView reloadData];
        
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

- (CGFloat)getHeight {
    [_collectionView reloadData];
    return self.layout.collectionViewContentSize.height;
}
 

//解决ios8上自动布局的问题

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
    return YES;
}


#pragma mark - 代理方法 Delegate Methods
// 设置分区

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

// 每个分区上得元素个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _personConsArr.count;
}

// 设置cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HEConsulItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: KHEConsulItemCell forIndexPath:indexPath];
    cell.model = _personConsArr[indexPath.row];
     return cell;
}

// 设置cell大小 itemSize：可以给每一个cell指定不同的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
 //    CGFloat width = [self gainStringWidthWithString:self.nameArray[indexPath.row] font:15.0f height:height];
    return CGSizeMake(101, 55);
}


// 设置UIcollectionView整体的内边距（这样item不贴边显示）
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    // 上 左 下 右
    return UIEdgeInsetsMake(15, 15 , 0, 15);
}

// 设置minimumLineSpacing：cell上下之间最小的距离
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 15;
}

// 设置minimumInteritemSpacing：cell左右之间最小的距离
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 15;
}

// 选中cell的回调
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
 
    if (self.selectBlock) {
        self.selectBlock(_personConsArr[indexPath.row]);
    }
    [self.navigationController popViewControllerAnimated:YES];
}
 



@end
